package com.ruoyi.weixin.service.impl;

import java.util.*;

import com.ruoyi.common.exception.ClientException;
import com.ruoyi.common.exception.api.LoginException;
import com.ruoyi.common.utils.mail.EmailUtils;
import com.ruoyi.framework.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.weixin.mapper.ClientMapper;
import com.ruoyi.weixin.domain.Client;
import com.ruoyi.weixin.service.IClientService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.DigestUtils;

/**
 * 客户Service业务层处理
 * 
 * @author cloudoer
 * @date 2019-09-03
 */
@Service
public class ClientServiceImpl implements IClientService 
{
    @Autowired
    private ClientMapper clientMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private EmailUtils emailUtils;


    /**
     * 查询客户
     * 
     * @param clientId 客户ID
     * @return 客户
     */
    @Override
    public Client selectClientById(String clientId)
    {
        Client client = new Client();
        client.setClientId(clientId);
        return clientMapper.selectClientById(client);
    }

    /**
     * 查询客户列表
     * 
     * @param client 客户
     * @return 客户
     */
    @Override
    public List<Client> selectClientList(Client client)
    {
        return clientMapper.selectClientList(client);
    }

    /**
     * 新增客户
     * 
     * @param client 客户
     * @return 结果
     */
    @Override
    public int insertClient(Client client)
    {
        return clientMapper.insertClient(client);
    }

    /**
     * 修改客户
     * 
     * @param client 客户
     * @return 结果
     */
    @Override
    public int updateClient(Client client)
    {
        return clientMapper.updateClient(client);
    }

    @Override
    public boolean clientBirthday(Client client) {
        Client c = clientMapper.selectClientById(client);
        if(c.getBirthday() != null){
            return false;
        }
        int temp = clientMapper.updateClient(client);
        if (temp > 0){
            return true;
        }
        return false;
    }

    /**
     * 删除客户对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteClientByIds(String ids)
    {
        return clientMapper.deleteClientByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除客户信息
     * 
     * @param clientId 客户ID
     * @return 结果
     */
    public int deleteClientById(String clientId)
    {
        return clientMapper.deleteClientById(clientId);
    }

    @Override
    public Client selectClientInfoById(Client client) {
        return clientMapper.selectClientInfoById(client);
    }

    @Override
    public boolean emailVerifi(String email) {
        Random random = new Random();
        String verifi = random.nextInt(1000000) + "";
        if (redisUtils.hasKey(email)) {
            redisUtils.del(email);
        }
        redisUtils.set(email, verifi, 600);
        emailUtils.sendSimpleMail(email, "验证码", "请在10分钟内输入验证码：" + verifi + ",请勿泄露验证码，如非本人操作请忽略本邮件");
        return true;
    }

    @Override
    public boolean emailBinding(String clientId, String email, String verifi) throws Exception {
        String verifiData = (String) redisUtils.get(email);
        if (verifi.equals(verifiData)){
            Client c = new Client();
            c.setEmail(email);
            List<Client> clients = clientMapper.selectClientList(c);
            if (clients != null && clients.size() > 0){
                throw new ClientException("这个邮箱已绑定其他账号");
            }
            Client client = new Client();
            client.setClientId(clientId);
            client.setEmail(email);
            clientMapper.updateClient(client);
            redisUtils.del(email);
            return true;
        }else {
            throw new ClientException("验证码错误");
        }
    }

    @Override
    public boolean loginPad(String clientId, String password, String email, String verifi) {
        String verifiData = (String) redisUtils.get(email);
        if (verifi.equals(verifiData)){
            String pad = DigestUtils.md5DigestAsHex(password.getBytes());
            Client client = new Client();
            client.setClientId(clientId);
            client.setLoginPad(pad);
            clientMapper.updateClient(client);
            redisUtils.del(email);
            return true;
        }
        return false;
    }

    @Override
    public Client emailLogin(String email, String verifi) {
        String verifiData = (String) redisUtils.get(email);
        if (verifi.equals(verifiData)){
            Client c = new Client();
            c.setEmail(email);
            Client client = clientMapper.selectClientInfoById(c);
            if (client == null){
                client = new Client(UUID.randomUUID().toString().replace("-",""));
                client.setNickName(email);
                client.setEmail(email);
                client.setGradeId(new Long(1));
                clientMapper.insertClient(client);
                return client;
            }else {
                return client;
            }
        }
        return null;
    }

    @Override
    public Client padLogin(String name, String password) throws LoginException {
        String pad = DigestUtils.md5DigestAsHex(password.getBytes());
        Client c = new Client();
        c.setEmail(name);
        c.setPhone(name);
        c.setLoginPad(pad);
        Client client = clientMapper.loginByPad(c);
        if (client == null){
            throw new LoginException("500", "密码或账号错误");
        }
        return client;
    }
}
